| Total Complexity | 6 | 
| Total Lines | 34 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
| 1 | import Field from "./Field"; | ||
| 4 | |||
| 5 | /** | ||
| 6 | * | ||
| 7 | */ | ||
| 8 | export default class Relation extends Field { | ||
| 9 | |||
| 10 | foreignKey: string; | ||
| 11 | |||
| 12 | model: ModelStaticInterface; | ||
| 13 | |||
| 14 |     constructor(model: ModelStaticInterface, foreignKey: string = null, name: string = null) { | ||
| 15 | const className = name ?? model.snakeCaseClassName; | ||
| 16 | super(className); | ||
| 17 | this.model = model; | ||
| 18 | this.foreignKey = foreignKey; | ||
| 19 | } | ||
| 20 | |||
| 21 |     set _value(value: unknown) { | ||
| 22 |         if (!Array.isArray(value)) { | ||
| 23 | value = [value]; | ||
| 24 | } | ||
| 25 |         value.forEach((modelValue) => { | ||
| 26 |             if (!(this.model.ids().includes(modelValue.id))) { | ||
| 27 | this.model.insert(modelValue); | ||
| 28 | } | ||
| 29 | }); | ||
| 30 | } | ||
| 31 | |||
| 32 |     getRelationalFields(): Array<ForeignKey> { | ||
| 33 | return [new ForeignKey(this.foreignKey).setRelation(this)]; | ||
| 34 | } | ||
| 35 | |||
| 36 |     tableSetup(table: TableInterface): void { | ||
| 37 | table.registerIndex(this.foreignKey); | ||
| 38 | } | ||
| 39 | } |